package tlias.Aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tlias.mapper.OperateLogMapper;
import tlias.pojo.OperateLog;
import tlias.utils.CurrentHolder;

import java.time.LocalDateTime;
import java.util.Arrays;


@Aspect
@Component
public class Record {
    @Autowired
    private OperateLogMapper operateLogMapper;
    @Around("@annotation(tlias.annotation.Crud)")
    public Object record(ProceedingJoinPoint joinPoint) throws Throwable {
        long  start = System.currentTimeMillis();
        Object obj = joinPoint.proceed();
        long end = System.currentTimeMillis();
        OperateLog log = new OperateLog();
        log.setOperateEmpId(CurrentHolder.getCurrentId());
        CurrentHolder.remove();
        log.setOperateTime(LocalDateTime.now());
        log.setClassName(joinPoint.getTarget().getClass().toString());
        log.setMethodName(joinPoint.getSignature().getName());
        log.setMethodParams(Arrays.toString(joinPoint.getArgs()));
        log.setReturnValue(obj.toString());
        log.setCostTime(end - start);
        operateLogMapper.insert(log);
        return obj;
    }
}
